<?php

namespace App\Http\Requests\dispatch;

use App\Http\ValidateResult;
use Illuminate\Support\Facades\Validator;

trait PurchaseRequest
{
    public static function add_check(array $params): ValidateResult {
        $validator = Validator::make($params, [
            'material_id' => 'required|integer|exists:t_dispatch_material,id',
            'purchase_number' => 'required|numeric',
        ], [
            'material_id.required' => '物料id不能为空',
            'purchase_number.required' => '请填写采购数量',
        ]);

        if ($validator->fails()) {
            return ValidateResult::error($validator->errors()->first());
        }
        return ValidateResult::correct();
    }

    public static function examine_check(array $params): ValidateResult {
        $validator = Validator::make($params, [
            'status' => 'required|integer|in:2,3'
        ], [
            'status.required' => '请选择审核结果',
            'status.in' => '审核结果不在范围内',
        ]);

        if ($validator->fails()) {
            return ValidateResult::error($validator->errors()->first());
        }
        return ValidateResult::correct();
    }

    public static function file_check(array $params): ValidateResult {
        $validator = Validator::make($params, [
            'file' => 'required|array'
        ], [
            'file.required' => '上传附件不能为空',
            'file.array' => '附件参数格式不正确',
        ]);

        if ($validator->fails()) {
            return ValidateResult::error($validator->errors()->first());
        }
        return ValidateResult::correct();
    }

}
